<?php

/**
 * @author kevin
 * @date 2016-01-16 14:00:00
 * @desc 支付操作
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$version = '1.0';   //版本号

if ($_POST['api_version'] != $version)      //网店的接口版本低
{
	api_err('0x008', 'a low version api');
}
if(empty($_POST['st'])){
	api_err('0x008', 'param st is Missing');
}
//用户ID检测
if(!isset($GLOBALS['uid'])){
	api_err('0x015', 'You have not logged on');
}
$user_id = $GLOBALS['uid'];
//支付方式列表
if($_POST['st'] == 'list'){
    //参数获取
    $order_id = trim($_POST['order_id']);
    if(empty($order_id)){
        api_err('0x008','param order_id can not empty.');
    }
	$order = get_order_detailv1($order_id, $user_id);
    if(empty($order)){
        api_err('0x008','order_id is not exists.');
    }
    $cod        = true;
    $cod_fee    = 0;
    $payment_list = available_payment_listv1(1, $cod_fee);
    $data['order_info'] = array(
        'order_sn' => $order['order_sn'],
        'goods_amount' => $order['goods_amount'],
        'shipping_fee' => $order['shipping_fee'],
        'order_amount' => $order['order_amount'],
    );
    $img = array(
        'alipayapp'=>array(
            'ios_img'=>'images/payicon/ios_alipay.png',
            'android_img'=>'images/payicon/android_alipay.png',
        ),
        'tenpayapp'=>array(
            'ios_img'=>'images/payicon/ios_tenpay.png',
            'android_img'=>'images/payicon/android_tenpay.png',
        ),
    );
    foreach($payment_list as $key=>$value){
        $payment_list[$key]['ios_img'] = get_image_pathv1('', $img[$value['pay_code']]['ios_img']);
        $payment_list[$key]['android_img'] = get_image_pathv1('', $img[$value['pay_code']]['android_img']);
    }
    $data['payment_list'] = $payment_list;
}
//支付请求接口
if($_POST['st'] == 'request'){
    //参数获取
    $order_id = trim($_POST['order_id']);
    if(empty($order_id)){
        api_err('0x008','param order_id can not empty.');
    }
    $pay_code = trim($_POST['pay_code']);
    if(empty($pay_code)){
        api_err('0x008', 'param pay_code can not empty.');
    }
    if(!in_array($pay_code, array('alipayapp','tenpayapp'))){
        api_err('0x008', 'param pay_code invalid');
    }
    include_once("includes/modules/payment/{$pay_code}.php");
    //判断订单是否支付 
	$order = get_order_detailv1($order_id, $user_id);
    if($order['status'] !== 0){
        api_err('0x008','order has been paid.');
    }
    //构造请求数据 
    $payment = get_pay_info($pay_code);
    $pay_config = unserialize_config($payment['pay_config']);
    $config = array_merge($alipay_config, $pay_config);
    $pay_obj = new $pay_code();
    $data = $pay_obj->get_code($order, $config);
    if(empty($data)){
        api_err('0x008', 'the error occurred.');
    }
}
//st参数错误
if(!isset($data)){
	api_err('0x008', 'param st is not exists');
}
//返回数据
data_back($data, '', RETURN_TYPE);
//查询支付方式
function get_pay_info($code){
    $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('payment') . " WHERE pay_code = '{$code}' AND enabled = '1'";
    return $GLOBALS['db']->getRow($sql);
}
